package com.masterclient.app.modules.goods.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.base.FragmentHostActivity;
import com.masterclient.app.base.MainViewHolder;
import com.eoner.baselibrary.bean.goods.CommodityDetailData;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.Utils;

import java.util.ArrayList;

public class CommodityDetailBannerAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    Activity mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;

    CommodityDetailData data;

    GoodsImageAdapter imageAdapter;


    public CommodityDetailBannerAdapter(Activity context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null, null);
    }

    public CommodityDetailBannerAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, CommodityDetailData data) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.data = data;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        ViewPager vp_goods = holder.itemView.findViewById(R.id.vp_goods);
        TextView accont_txt1 = holder.itemView.findViewById(R.id.accont_txt1);
        TextView accont_txt2 = holder.itemView.findViewById(R.id.accont_txt2);
        ImageView ivActivity = holder.itemView.findViewById(R.id.iv_activity);

        Utils.setDin(accont_txt1, mContext);
        Utils.setDin(accont_txt2, mContext);

        imageAdapter = new GoodsImageAdapter(data.getProduct_id(), data.getTitle(), data.getImages(), mContext, data.getVideo(), new GoodsImageAdapter.OnClickViewListener() {
            @Override
            public void getPosition(int position) {
                Fragment fragment = (Fragment) ARouter.getInstance().build(AroutePath.LOOK_PICTURE_FRAGMENT)
                        .withStringArrayList("urlList", (ArrayList<String>) data.getImages())
                        .withString("product_id", data.getProduct_id())
                        .withString("product_name", data.getTitle())
                        .withInt("position", position)
                        .navigation();
                FragmentHostActivity.openFragment(mContext, fragment);
            }
        });
        vp_goods.setAdapter(imageAdapter);

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 20);
        lp.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.vp_goods);
        lp.setMargins(0, 0, 0, ScreenUtils.dp2px(14));
        accont_txt1.setText("1");
        accont_txt2.setText("/" + imageAdapter.getCount());

        //图片上活动图标
        double ratio = 1;
        if (!TextUtils.isEmpty(data.getImage_width())) {
            ratio = ScreenUtils.getScreenWidth() / Double.parseDouble(data.getImage_width());
        }
        if (data.getIcon_info() != null
                && !TextUtils.isEmpty(data.getIcon_info().iconHeight)
                && !TextUtils.isEmpty(data.getIcon_info().iconWidth)) {
            int leftMargin = (int) (Integer.parseInt(data.getIcon_info().iconLeftMargin) * ratio);
            int topMargin = (int) (Integer.parseInt(data.getIcon_info().iconTopMargin) * ratio);
            int bottomMargin = (int) (Integer.parseInt(data.getIcon_info().iconBottomMargin) * ratio);
            ViewTransformUtil.glideImageView(mContext, data.getIcon_info().iconImage, ivActivity, R.mipmap.defaults);
            ivActivity.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivActivity.getLayoutParams();
            params.width = (int) (Integer.valueOf(data.getIcon_info().iconWidth) * ratio);
            params.height = (int) (Integer.valueOf(data.getIcon_info().iconHeight) * ratio);
            params.addRule(RelativeLayout.ALIGN_BOTTOM, 0);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
            params.setMargins(0, 0, 0, 0);
            switch (data.getIcon_info().iconMarginType) {
                case "1":
                    params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.vp_goods);
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    params.setMargins(leftMargin, topMargin, 0, bottomMargin);
                    break;
                case "2":
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    params.setMargins(leftMargin, topMargin, 0, bottomMargin);
                    break;
                default:
                    break;
            }
            ivActivity.setLayoutParams(params);
        } else {
            ivActivity.setVisibility(View.GONE);
        }

        vp_goods.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                accont_txt1.setText("" + (position + 1));
                if (position == 0) {
                    ivActivity.setVisibility(View.VISIBLE);
                } else {
                    ivActivity.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    public void destroy() {
        if (imageAdapter != null) {
            imageAdapter.destroy();
        }
    }
}
